/*
 * @LastEditors: yuSeen
 * @LastEditTime: 2022-01-10 16:58:14
 */
import { defineModule } from "concent";
import { useLocalStorage } from "@/hooks/localStorage/useLocalStorage";
import { useSong } from '@/hooks/song/useSong'
import { IPlayStore, playMode, songTime } from "@/types/store/playStoreType";
import { IAllObject } from "@/types/base";
import { notEmptyObj } from "@/utils/util";
const { getStorageItem, setStorageItem } = useLocalStorage()
const { getSongName, getSingers, getSongTime } = useSong()

const playStore = defineModule({
  state: <IPlayStore>{
    songId: getStorageItem("songId"),
    url: undefined,
    volume: getStorageItem("volume") || 1,
    detail: getStorageItem("songDetail") || {},
    lyric: getStorageItem("lyric") || "暂无歌词",
    isPlaying: false,
    inPlayPage: false,
    playList: getStorageItem("playList") || [],
    playHistory: getStorageItem("playHistory") || [],
    currentPlayTime: 0,
    playMode: getStorageItem("playMode") || playMode.顺序播放,
  },
  computed: {
    // 获取歌曲名称
    name({detail}): string {
      if(notEmptyObj(detail)) return getSongName(detail.name, detail.alia)
      return '未知作品'
    },
    // 获取封面
    cover({detail}): string | undefined {
      if(notEmptyObj(detail)) return detail.al.picUrl
      return undefined
    },
    // 获取歌手
    singer({detail}): string {
      if(notEmptyObj(detail)) return getSingers(detail.ar)
      return '未知艺术家'
    },
    // 获取歌曲时间
    songTime({detail}): songTime {
      if(notEmptyObj(detail)) return {
        time: Number((detail.dt / 1000).toFixed(2)),
        timeStr: getSongTime(detail.dt)
      }
      return {
        time: 0,
        timeStr: '00:00'
      }
    }
  },
  reducer: {
    // 设置是否处于歌曲详情页面，不传状态则取反
    toggleInPlayPage(flag?: boolean, moduleState?: IPlayStore) {
      if(flag === undefined) return {inPlayPage: !moduleState?.inPlayPage}
      return {inPlayPage: flag}
    },
    // 设置是否正在播放中，不传状态则取反
    toggleIsPlaying(flag?: boolean, moduleState?: IPlayStore) {
      if(flag === undefined) return {isPlaying: !moduleState?.isPlaying}
      return {isPlaying: flag}
    },
    // 设置id
    setId(id: number) {
      setStorageItem('songId', id)
      return {songId: id}
    },
    // 设置音量
    setVolume(volume: number) {
      setStorageItem('volume', volume)
      return {volume}
    },
    // 设置播放地址
    setUrl(url?: string) {
      return {url}
    },
    // 设置播放时间
    setCurrentPlayTime(time: number) {
      return {currentPlayTime: time}
    },
    // 设置详情
    setDetail(detail: IAllObject) {
      setStorageItem('songDetail', detail)
      return {detail}
    },
    // 设置歌词
    setLyric(lyric: string) {
      setStorageItem('lyric', lyric)
      return {lyric}
    },
    // 设置播放模式
    setPlayMode(mode: playMode) {
      setStorageItem('playMode', mode)
      return {playMode: mode}
    },
    // 设置播放列表
    setPlayList(list: any[]) {
      setStorageItem('playList', list)
      return {playList: list}
    },
    // 设置播放记录列表
    setPlayHistory(list: number[]) {
      setStorageItem('playHistory', list)
      return {playHistory: list}
    }
  },
});

export default playStore;
